﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    <link href="@Url.Content("~/lib/summernote/0.8.15/summernote.min.css")" rel="stylesheet" type="text/css">
    <script src='@Url.Content("~/lib/summernote/0.8.15/summernote.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/lib/summernote/0.8.15/lang/summernote-zh-CN.min.js")' type="text/javascript"></script>

    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/imageupload/1.0/css/imgup.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/imageupload/1.0/js/imgup.min.js"))
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-2 control-label ">轮播图名称<font class="red"> *</font></label>
            <div class="col-sm-10">
                <input id="advertisement_name" col="advertisement_name" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">
                <span>轮播图</span><font class="red"> *</font><br />
                <span>(宽高比 1.5：1)</span>
            </label>
            <div class="col-sm-10">
                <div id="advertisement_url" class="img-box">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">广告类型<font class="red"> *</font></label>
            <div class="col-sm-4" id="IsActivity" col="IsActivity"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">活动</label>
            <div class="col-sm-4" id="SysNewsId" col="SysNewsId"></div>
        </div>
        <div class="form-group" style="display:none">
            <label class="col-sm-2 control-label ">门店</label>
            <div class="col-sm-4" id="shopId" col="shopId"></div>
        </div>
        <div class="form-group" style="display:none">
            <label class="col-sm-2 control-label ">知识</label>
            <div class="col-sm-4" id="knowledgepostsId" col="knowledgepostsId"></div>
        </div>
        <div class="form-group" style="display:none">
            <label class="col-sm-2 control-label ">课程</label>
            <div class="col-sm-4" id="coursesId" col="coursesId"></div>
        </div>
        @*<div class="form-group">
            <label class="col-sm-2 control-label ">跳转地址</label>
            <div class="col-sm-10">
                <input id="url" col="url" type="text" class="form-control" />
            </div>
        </div>*@
        <div class="form-group">
            <label class="col-sm-2 control-label ">排序</label>
            <div class="col-sm-10">
                <input id="sort" col="sort" type="text" class="form-control" />
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");

    $(function () {

        $("#IsActivity").ysRadioBox({
            data: ys.getJson(@Html.Raw(typeof(advertisement_type_enum).EnumToDictionaryString())),
            default: '@advertisement_type_enum.活动广告.ParseToInt()'
        });

        $("#SysNewsId").ysComboBox({
            url: '@Url.Content("~/OrganizationManage/News/GetListJson")',
            key: "Id",
            value: "NewsTitle",
            class: "form-control",
        });

        $("#shopId").ysComboBox({
            url: '@Url.Content("~/QixingManage/Shop/GetListJson")',
            key: "Id",
            value: "shop_name",
            class: "form-control",
        });

        $("#knowledgepostsId").ysComboBox({
            url: '@Url.Content("~/QixingManage/KnowledgePosts/GetListJson")',
            key: "Id",
            value: "title",
            class: "form-control",
        });

        $("#coursesId").ysComboBox({
            url: '@Url.Content("~/QixingManage/Courses/GetListJson")',
            key: "Id",
            value: "title",
            class: "form-control",
        });


        getForm();

        $('#form').validate({
            rules: {
                advertisement_name: { required: true }
            }
        });

        $("#advertisement_url").imageUpload({ uploadImage: 'uploadThumbImage', limit: 1, context: ctx });

        // 绑定点击事件
        $("#IsActivity input[type='radio']").on('ifClicked', function(event) {
            var value = $(this).val();
            if (value == 0) {
                $("#shopId").parent().show();
                $("#SysNewsId").parent().hide();
                $("#knowledgepostsId").parent().hide();
                $("#coursesId").parent().hide();
            }
            else if(value ==1){
                $("#shopId").parent().hide();
                $("#SysNewsId").parent().show();
                $("#knowledgepostsId").parent().hide();
                $("#coursesId").parent().hide();
            }
            else if (value ==2) {
                $("#shopId").parent().hide();
                $("#SysNewsId").parent().show();
                $("#knowledgepostsId").parent().hide();
                $("#coursesId").parent().hide();
            }
            else if (value == 3) {
                $("#shopId").parent().hide();
                $("#SysNewsId").parent().hide();
                $("#knowledgepostsId").parent().show();
                $("#coursesId").parent().hide();
            }
            else
            {
                $("#knowledgepostsId").parent().hide();
                $("#shopId").parent().hide();
                $("#SysNewsId").parent().hide();
                $("#coursesId").parent().show();
            }
        });
    });


    function getForm() {
        if (id > 0) {
            ys.ajax({
                url: '@Url.Content("~/QixingManage/Advertisement/GetFormJson")' + '?id=' + id,
                type: 'get',
                success: function (obj) {
                    if (obj.Tag == 1) {
                        $("#advertisement_url").imageUpload("setImageUrl", obj.Data.advertisement_url);

                        if (obj.Data.IsActivity == "1") {
                            $("#shopId").parent().hide();
                            $("#SysNewsId").parent().show();
                            $("#knowledgepostsId").parent().hide();
                            $("#coursesId").parent().hide();
                        }
                       else if (obj.Data.IsActivity == "0") {
                            $("#shopId").parent().show();
                            $("#SysNewsId").parent().hide();
                            $("#knowledgepostsId").parent().hide();
                            $("#coursesId").parent().hide();
                        }
                        else if (obj.Data.IsActivity == "2") {
                            $("#shopId").parent().show();
                            $("#SysNewsId").parent().hide();
                            $("#knowledgepostsId").parent().hide();
                            $("#coursesId").parent().hide();
                        }
                        else if (obj.Data.IsActivity == "3") {
                            $("#shopId").parent().hide();
                            $("#SysNewsId").parent().hide();
                            $("#knowledgepostsId").parent().show();
                            $("#coursesId").parent().hide();
                        }
                        else
                        {
                            $("#shopId").parent().hide();
                            $("#SysNewsId").parent().hide();
                            $("#knowledgepostsId").parent().hide();
                            $("#coursesId").parent().show();
                        }

                        $('#form').setWebControls(obj.Data);
                    }
                }
            });
        }
        else {
            var defaultData = {};
            $('#form').setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id });
            postData.advertisement_url = $("#advertisement_url").imageUpload("getImageUrl");
            postData.type = 1;

            if (postData.IsActivity == "0") {
                postData.url = "/myPackage/pages/shops/shops";
            }
            if (postData.IsActivity == "1") {
                postData.url = "/myPackage/pages/activityDetails/activityDetails";
            }
            if (postData.IsActivity == "2") {
                postData.url = "/myPackage/pages/bicycleDetails/bicycleDetails";
            }
            if (postData.IsActivity == "3") {
                postData.url = "/storePackage/pages/school/knowledgeDetails";
            }
            if (postData.IsActivity == "4") {
                postData.url = "/storePackage/pages/school/CourseDetails";
            }
            //if (postData.type==2)
            //{
            //    if (ys.isNullOrEmpty(postData.shopid))
            //    {
            //        return ys.msgError("请选择商铺!");
            //    }
            //}
            ys.ajax({
                url: '@Url.Content("~/QixingManage/Advertisement/SaveFormJson")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }

     function uploadNewsImage(file, editor, welEditable) {
        var formdata = new FormData();
        formdata.append("fileList", file);
        ys.ajaxUploadFile({
            url: '@GlobalContext.SystemConfig.ApiSite' + '/File/UploadFile?fileModule=@UploadFileType.News.ParseToInt()',
            data: formdata,
            success: function (obj) {
                if (obj.Tag == 1) {
                    $("#newsContent").summernote('insertImage', '@GlobalContext.SystemConfig.ApiSite' + obj.Data, '/');
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        })
    }

    function uploadThumbImage(file, callback) {
        var formdata = new FormData();
        formdata.append("fileList", file);
        ys.ajaxUploadFile({
            url: '@GlobalContext.SystemConfig.ApiSite' + '/File/UploadFile?fileModule=@UploadFileType.News.ParseToInt()',
            data: formdata,
            success: function (obj) {
                if (obj.Tag == 1) {
                    if (callback) {
                        callback('@GlobalContext.SystemConfig.ApiSite' + obj.Data);
                    }
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        })
    }
</script>

